{literal}<?php{/literal}
require_once '{$entity->project->slug}.php';

/**
 * @package {$entity->project->slug}
 * @subpackage controller
 */
class {$entity->plural|capitalize:true}_protoweb extends {$entity->project->slug|capitalize:true}
{

	public function __construct() 
	{
		parent::__construct();
	}

    protected function createPrepare(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('createPrepare')}
    {/foreach}

        // Collections of has one relationships
    {foreach from=$entity->getHasOne() item="oneRelated"}
    $data['{$oneRelated->plural}'] = new {$oneRelated->singular|capitalize:true}();
        $data['{$oneRelated->plural}']->get();
    {/foreach}

        // Collections of has many to many relationships
    {foreach from=$entity->getHasManyToMany() item="manyRelated"}
    $data['{$manyRelated->plural}'] = new {$manyRelated->singular|capitalize:true}();
        $data['{$manyRelated->plural}']->get();
    {/foreach}

    }

    protected function assignCreatePost(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('assignCreatePost')}
    {/foreach}

        // has one relationships
    {foreach from=$entity->getHasOne() item="oneRelated"}
    if ($this->input->post('{$oneRelated->singular}')) {
            $data['{$oneRelated->singular}'] = new {$oneRelated->singular|capitalize:true}($this->input->post('{$oneRelated->singular}'));
        }
    {/foreach}
    
        // Many to many
    {foreach from=$entity->getHasManyToMany() item="manyRelated"}
    $data['{$manyRelated->plural}_save'] = $this->input->post('{$manyRelated->plural}');
    {/foreach}

        return true;
    }

    protected function afterCreateSuccess(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('afterCreateSuccess')}
    {/foreach}
    
    }

    protected function revertOnCreateError(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('revertOnCreateError')}
    {/foreach}
    
    }

    protected function updatePrepare(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('updatePrepare')}
    {/foreach}

        // Collections of has one relationships
    {foreach from=$entity->getHasOne() item="oneRelated"}
    $data['{$oneRelated->plural}'] = new {$oneRelated->singular|capitalize:true}();
        $data['{$oneRelated->plural}']->get();
    {/foreach}
    
        // Collections of has many to many relationships
    {foreach from=$entity->getHasManyToMany() item="manyRelated"}
    $data['{$manyRelated->plural}'] = new {$manyRelated->singular|capitalize:true}();
        $data['{$manyRelated->plural}']->get();
    {/foreach}

    }

    protected function assignUpdatePost(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('assignUpdatePost')}
    {/foreach}

        // has one relationships
    {foreach from=$entity->getHasOne() item="oneRelated"}
    if ($this->input->post('{$oneRelated->singular}')) {
            $data['{$oneRelated->singular}'] = new {$oneRelated->singular|capitalize:true}($this->input->post('{$oneRelated->singular}'));
        }

    {/foreach}

        // Many to many
    {foreach from=$entity->getHasManyToMany() item="manyRelated"}
    $data['{$manyRelated->plural}_save'] = $this->input->post('{$manyRelated->plural}');
    {/foreach}    
    
        return true;
    }

    protected function afterUpdateSuccess(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('afterUpdateSuccess')}
    {/foreach}
    
    }

    protected function revertOnUpdateError(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('revertOnUpdateError')}
    {/foreach}
    
    }

    protected function afterDeleteSuccess(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('afterDeleteSuccess')}
    {/foreach}
    
    }

    protected function readPrepare(&$data)
    {
    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('readPrepare')}
    {/foreach}
    
    }

    protected function saveEntity(&$data,$saveHasOne=true,$saveHasMany=true)
    {
        // saving attributes
        if ( ! $data['{$entity->singular}']->save()) {
            return false;
        }

        // saving one raleted
        if ($saveHasOne) {
            $relationships = array();
            {foreach from=$entity->getHasOne() item="oneRelated"}
    if (array_key_exists('{$oneRelated->singular}', $data)) {
                array_push($relationships, $data['{$oneRelated->singular}']);
            }
            {/foreach}

            if ( ! $data['{$entity->singular}']->save($relationships)) {
                return false;
            }
        }

        // saving many to many relationships
        if ($saveHasMany) {
            {foreach from=$entity->getHasManyToMany() item="manyRelated"}
    // {$manyRelated->plural} 
            if (array_key_exists('{$manyRelated->plural}_save', $data)) {
                $data['{$entity->singular}']->updateManyToManyWithIdArray('{$manyRelated->singular}',$data['{$manyRelated->plural}_save']);
            }

            {/foreach}
        }
        return true;
    }

    {foreach from=$entity->attribute item="attribute"}
    {$attribute->getTemplate('controllerMethods')}
    {/foreach}

} // class {$entity->plural|capitalize:true}

/* End of file {$entity->plural}_protoweb.php */
/* Location: ./application/controllers/{$entity->plural}_protoweb.php */